package net.lesscoding.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * @author eleven
 * @date 2024/10/28 13:59
 * @apiNote
 */
@Slf4j
public class NettyFutureTest {

    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();
        Future<Integer> future = eventLoop.submit(() -> {
            log.info("执行任务");
            Thread.sleep(1000);
            return new Random().nextInt();
        });
        //log.info("当前结果 {}", future.getNow());
        //log.info("等待结果");
        //log.info("结果是：{}", future.get());

        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.info("接受结果 {}", future.getNow());
            }
        });
    }
}
